<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <style>
         .bcred {
            /*background-color: pink;*/
            border: 1px solid black;
            border-radius: 5px;
             width: 500px;
             height: 380px;
             float: none;
             margin: 1% auto;
        }
         .bcred div {
             margin-top: 5%;
         }
    </style>
</head>
<body>
    <div class="nav">
        <nav class="navbar navbar-inverse">
        <div class="container">
          <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
              <li><a href="/">登录</a></li>
              <li><a href="/register">注册</a></li>
              <li><a href="/weather">天气</a></li>
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </nav>
    </div>
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8 bcred">
            <div class="username">
            <label>用户名</label>
<!--                <input type="text" placeholder="Username" name="username" id="username">-->
            <input type="text" name="username" id="username" placeholder="5-8位字母"  class="form-control" />
            </div>
            <div class="password">
                <label>密码</label>
<!--                <input type="password" placeholder="Password" name="password" id="password">-->
                <input type="password" name="password" id="password" placeholder="5-8位数字与字母" class="form-control" />
            </div>
            <div class="password">
                <label>确认密码</label>
<!--                <input type="password" placeholder="Password" name="password" id="password">-->
                <input type="password" name="password" id="repassword" placeholder="5-8位数字与字母" class="form-control" />
            </div>
            <div class="message">
                <h5 id="tipMessage"></h5>
            </div>
<!--            <button type="submit" id="login-button">Login</button>-->
            <div class="loginButton">
                <button type="submit" class="btn btn-success" style="width: 100%" id="isTrue">注册</button>
            </div>
        </div>
        <div class="col-md-2"></div>
    </div>
</body>
<script>
    # jquery函数的初始化
    $(document).ready(function(){

      $("#isTrue").click(function(){
          username = $('#username').val()
          password = $('#password').val()
          relpassword = $('#repassword').val()
          if(!relpassword || !password || !username){
              $('#tipMessage').text('请输入用户名密码')
          }

          else if(relpassword != password){
              $('#tipMessage').text('两次密码不相同')
          }

          else if(username.length>8
              || username.length <5
              || password.length > 8
              || password.length < 5
              || relpassword.length >8
              || relpassword.length <5
          ){
              $('#tipMessage').text('用户名密码格式不正确')
          }

          else {
              var userList={
                  "username": username,
                  "password": password,
                  "relpassword": relpassword,
                }
             $.ajax({
                    type:"POST",
                    url:"/register",
                    contentType: "application/json", //必须这样写
                    dataType:"json",
                    data:JSON.stringify(userList),//schoolList是你要提交是json字符串
                    success:function (data) {
                        // console.log(data['msg'])
                        if(data['msg'] == '注册成功'){
                            $('#tipMessage').text(data['msg']+' 3秒后跳转')
                            setTimeout(function () {
                                window.location.href="/"
                            }, 3000);
                        }
                        else {
                            $('#tipMessage').text(data['msg'])
                        }
                    }
                })
          }
      });
    });
</script>
</html>